<?
class player extends game
{
	protected $id = 0;
	protected $pseudo = "" ;
	protected $stat = array();
	protected $game_stat = array();	
	protected $pigeon = -1;
	protected $avatar = NULL;
		
	//constructeur
	function __construct($pseudo,$id) {
		$this->id = $id; 		
		$this->pseudo = $pseudo;
		if ($this->check_player_name() == false){
			$this->create_player();		
		}
		$this->load_stat();
	}
	//accesseurs
  	function __get($name) {
    		return $this->$name;
  	}
  	function __set($name,$value) {
    		$this->$name=$value;
  	}
	protected function bois(){
		$this->game_stat["nb_coup_bu"]++;
	}
	private function check_player_name(){
		return mysql_num_rows(mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'"));	
	}

	private function create_player(){
		mysql_query("INSERT INTO players (pseudo) VALUES ('".$this->pseudo."')");
	}
	protected function load_stat(){
		$query = mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'");
		while ($stat = mysql_fetch_array($query)){ 		
			$this->stat["nb_partie"] = $stat['nb_partie'];
			$this->stat["nb_coup_bu"] = $stat['nb_coup_bu'];
			$this->stat["nb_coup_donne"] = $stat['nb_coup_donne'];
			$this->avatar =	$stat['avatar']; 	
		}
	}
	protected function update_stat(){
	//ajoute les stats de la partie en cours a la base
		$query = mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'");
		while ($stat = mysql_fetch_array($query)){ 
			$parti = $stat['nb_partie'];
			$coup_bu = $stat['nb_coup_bu'];
			$coup_donne = $stat['nb_coup_donne'];
		}
	
		mysql_query("UPDATE players SET nb_partie = ".($parti+1).",nb_coup_bu = ".($coup_bu+$this->game_stat["nb_coup_bu"]).",date_derniere_partie=now(),nb_coup_donne = ".($coup_donne+$this->game_stat["nb_coup_donne"])." WHERE pseudo like '".$this->pseudo."'")  or die ('Erreur : '.mysql_error());
			
	
	}
}
?>
